<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][1] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	jara_page_start("Write Page - Administrator CP");
	if(isset($_REQUEST["write_action"])) {
		try {
			$title = $_POST["title"];
			$text = $_POST["text"];
			if(!get_magic_quotes_gpc()) {
				$title = addslashes($title);
				$text = addslashes($text);
			}
			$query = "insert into jara_pages values('0', '$title', '$text', '0')";
			if(isset($_POST["menu"])) {
				$query = "insert into jara_pages values('0', '$title', '$text', '1')";
			}
			$result = jara_db_query($query);
			if($result == 0) {
				throw new JaraGeneralException("Couldn't add the page $title to the database.");
			}
			else {
				echo "<p>Added page successfully.<br /></p>";
				jara_page_end();
				exit;
			}
		}
		catch(JaraDatabaseException $ex) {
?>
	<p>
		A database error occured during the process of creating this page.
	</p>
	<p>
		Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
		Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
	</p>
<?php
			jara_page_end();
		}
		catch(JaraGeneralException $ex) {
?>
	<p>
		A general error occured during the process of creating this page.
	</p>
	<p>
		Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
	</p>
<?php
			jara_page_end();
		}
		exit;
	}
?>
<form action="write_page.php" method="post">
<p>
<input type="hidden" name="write_action" id="write_action" value="post" />
Page title: <input type="text" id="title" name="title" style="width: 500px" /><br />
Write your page:<br />
<textarea name="text" id="text" cols="60" rows="8"></textarea>
<script language="JavaScript" type="text/javascript" src="scripts/wysiwyg.js"></script>
<script language="JavaScript" type="text/javascript">WYSIWYG.attach('text');</script>
<br /><br />
<input type="checkbox" name="menu" id="menu" checked="checked" /> Display in Menu<br />
<input type="submit" value="Post!" />
</p>
</form>
<?php
	jara_page_end();
?>